package com.jams.goodox.adapter;

import android.content.Context;
import android.widget.ImageView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.jams.goodox.R;
import com.jams.goodox.entity.User;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;

import java.util.List;

/**
 * 支付通用适配器
 * Created by guozhen.hou on 2017/7/5.
 */

public class PayTypeAdapter extends ListCommonAdapter<Integer>{
    private int selectPayTypePosition = -1;
    private double balance;
    private double needPayMoney;

    public PayTypeAdapter(Context context, List<Integer> mDatas) {
        super(context, mDatas,  R.layout.pay_type);
    }

    public void setSelectionPosition(int selectPayTypePosition){
        this.selectPayTypePosition=selectPayTypePosition;
        notifyDataSetChanged();
    }

    public int getPayType() {//支付方式选择 -99余额支付 0:支付宝    1:银联  2:微信  5;线下支付
        if (selectPayTypePosition >= mDatas.size()) return -1;
        return mDatas.get(selectPayTypePosition);
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**
     * 设置应付金额
     *
     * @param needPayMoney 应付金额
     */
    public void setNeedPayMoney(double needPayMoney) {
        this.needPayMoney = needPayMoney;
    }

    @Override
    public void convert(ListCommonViewHolder helper, Integer item, int position) {
        ImageView imageView = helper.getView(R.id.iv_choose);
        if (selectPayTypePosition == position) {
            imageView.setBackgroundResource(R.mipmap.check_theme);
        } else {
            imageView.setBackgroundResource(R.mipmap.uncheck_icon);
        }
        helper.setViewVisible(R.id.balance,item==-99);//仅余额支付时，显示金额
        helper.setViewVisible(R.id.tv_pay_tip,item!=-99);
        switch (item) {
            case 0://支付宝
                helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_zfb);
                helper.setText(R.id.tv_pay_name, "支付宝支付");
                helper.setText(R.id.tv_pay_tip, "推荐有支付宝账户的用户使用");
                break;
            case 1://银联
                helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_yl);
                helper.setText(R.id.tv_pay_name, "银联支付");
                helper.setText(R.id.tv_pay_tip, "各大银行无卡支付、快捷支付");
                break;
            case 2://微信
                helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_wx);
                helper.setText(R.id.tv_pay_name, "微信支付");
                helper.setText(R.id.tv_pay_tip, "微信安全支付");
                break;
            case -99://余额
                helper.getView(R.id.iv_pay_icon).setBackgroundResource(R.mipmap.order_pay_balance);
                helper.setText(R.id.tv_pay_name, "账户余额支付");
                helper.setText(R.id.balance, User.getPriceString(balance));
                break;
        }
        helper.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int positions) {
                if (checkBalanceIsValid()) {
                    selectPayTypePosition = positions;
                    notifyDataSetChanged();
                }
            }
        });
    }

    //检查余额支付
    private boolean checkBalanceIsValid() {
        //当金额为0时，提示
        if (balance < needPayMoney || balance == 0) {
            new MaterialDialog.Builder(mContext).content("您的账户余额不足，请选择其他支付方式或去企业账号先充值再进行支付").positiveText("知道了").show();
            return false;
        }
        return true;
    }
}
